-- UIPayAmountInput
-- myCard支付页面
-- create by huanghr

UIPayAmountInput = class("UIPayAmountInput", function()
    return cc.Layer:create();
end);

-- pt商品编号
local PT_GOODS_ID = 10101;

function UIPayAmountInput.create()
    return UIPayAmountInput.new();
end

-- 构造函数
function UIPayAmountInput:ctor()
    self:setName("UIPayAmountInput");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/market/PayAmountInput.csb");
    self:addChild(node);
    self.node = node;

    if DungeonM.isInDungeon() then
        -- 迷宫中层级要调高
        self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 11);
    end

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册各种点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIPayAmountInput:registerTouchEvent()
    local node = self.node;
    local CT = findChildByName(self.node, "CT");

    -- 注册背景点击事件(点击背景，取消当前)
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIPayAmountInput");
        end
    end
    bgImg:addTouchEventListener(onBgClick);

    -- 确定按钮
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:tryRecharge();
            UIMgr.getCurrentScene():removeFormByName("UIPayAmountInput");
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("confirm"));

    -- 取消
    local btnCancel = findChildByName(self.node, "CT/btn_cancel");
    local function onCancelClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIPayAmountInput");
        end
    end
    btnCancel:addTouchEventListener(onCancelClick);
    TextStyleM.setTextStyle(btnCancel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnCancel:setTitleText(getLocStr("cancel"));
end

-- 注册事件处理回调函数
function UIPayAmountInput:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIPayAmountInput 界面析构清理");
            EventMgr.removeAll("UIPayAmountInput");
        end
    end);
end

-- 重绘
function UIPayAmountInput:redraw()
    local CT = findChildByName(self.node, "CT");

    local titleLabel = findChildByName(CT, "title_label");

    -- TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_WHITE);
    titleLabel:setFontSize(TextStyleM.TEXT_SIZE_MIDDLE);
    titleLabel:setColor(TextStyleM.TEXT_COLOR_WHITE);
    
    local titleStr = getLocStr("enter_pay_amount");
    titleLabel:setString(titleStr);

    -- 重绘输入框
    local bg1 = findChildByName(self.node, "CT/bg2");
    local edit1 = findChildByName(self.node, "CT/edit");

    -- 创建两个新的editBox
    local editEx1 = ccui.EditBox:create(edit1:getContentSize(), "images/ui/login/1.png");
    editEx1:setAnchorPoint(cc.p(0.5, 0.5));
    editEx1:setPosition(edit1:getPosition());

    editEx1:setFontName(getLocFontName());
    editEx1:setFontSize(TextStyleM.TEXT_SIZE_TINY);
    editEx1:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_NORMAL);
    editEx1:setMaxLength(16);
    editEx1:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);

    self.editEx1 = editEx1;

    edit1:getParent():addChild(editEx1);

    edit1:setVisible(false);

    bg1:setVisible(true);
    self.editEx1:setVisible(true);

    -- 设置输入框默认文字
    self.editEx1:setPlaceHolder(getLocStr("pay_amount_tip"));
    self.editEx1:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_GRAY2);
    self.editEx1:setPlaceholderFontSize(TextStyleM.TEXT_SIZE_SMALL2);
    self.editEx1:setFontColor(TextStyleM.TEXT_OUTLINE_COLOR_DARK);
    self.editEx1:setText("");
end

-- 适配
function UIPayAmountInput:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end

-- 充值
function UIPayAmountInput:tryRecharge()
    -- 充值
    local amount = tonumber(self.editEx1:getText());

    amount = math.floor(amount);

    if amount < 50 or amount > 5000 then 
        alert(getLocStr("pay_amount_tip"));
        return; 
    end

    MarketM.buyByRmb(PT_GOODS_ID, "mycard", amount);
end